package com.istudy.dao.impl;

import com.istudy.dao.BaseDao;
import com.istudy.dao.interfaces.WatchDao;
import com.istudy.entity.User;
import com.istudy.entity.Watch;
import com.istudy.utils.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class WatchDaoImpl implements WatchDao {
    private BaseDao baseDao = new BaseDao();
    /**
     * 获取展示列表
     *
     * @return 实体类 集合
     * @throws Exception 可能出现的错误
     */
    @Override
    public List getlistByUserID(User user, Page page) throws Exception {
        List<Watch> list = new ArrayList<Watch>();
       Watch watch =null;
        String sql = "SELECT a.id,a.`user_id`,b.`user_name` ," +
                "a.`video_id`,c.`video_name` ,a.`watch_time`" +
                " FROM `watch` AS a JOIN `user` AS b ,`video` AS c " +
                "WHERE a.`user_id`=b.`user_id` AND a.`video_id`=" +
                "c.`video_id` AND a.`user_id` =? ORDER BY a.`watch_time` DESC LIMIT ?,5";
        List<Object> list1=new ArrayList<>();
        list1.add(user.getUserId());
       list1.add(page.getPageSize()*(page.getPageNo()));
        ResultSet rs = baseDao.executeQuery(sql,list1.toArray());
        while (rs.next()) {
            watch = new Watch();
            fill(watch, rs);
            list.add(watch);
        }
        return list;
    }

    @Override
    public int getCount(User user) {
        String sql="SELECT COUNT(1) FROM watch WHERE user_id=?";
        List<Object> o=new ArrayList<>();
        o.add(user.getUserId());
        BaseDao base=new BaseDao();
        ResultSet rs= base.executeQuery(sql,o.toArray());
        int count=0;
        try {
            if(rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    private void fill( Watch watch, ResultSet rs) throws SQLException {
        watch.setId(rs.getInt("id"));
       watch.setUserId(rs.getInt("user_id"));
        watch.setVideoId(rs.getInt("video_id"));
        watch.setUserName(rs.getString("user_name"));
        watch.setVideoName(rs.getString("video_name"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
            Date date = (Date)rs.getObject("watch_time");
            watch.setWatchTime(simpleDateFormat.format(date));
    }
    /**
     * 添加记录
     *
     * @param watch 视频实体类
     * @return
     */
    @Override
    public int insertWatch(Watch watch) {
        String sql = "INSERT INTO watch ( `user_id`,`video_id`,`watch_time`)" +
                "VALUES (?,?,?)";
        List<Object> objects=new ArrayList<>();
        objects.add(watch.getUserId());
        objects.add(watch.getVideoId());
        objects.add(watch.getWatchTime());
       int i=-1;
       i=baseDao.executeUpdate(sql,objects.toArray());
        return i;
    }

    /**
     * 根据ID删除
     *
     * @param id id
     * @return 受影响行数
     */
    @Override
    public int delWatch(int id) {
        String sql = "DELETE FROM `watch` WHERE `id` =?";
        Object[] object = new Object[]{id};
        return baseDao.executeUpdate(sql,object);
    }
}
